
package com.hmsg.health.dao;

import com.hmsg.health.bean.Dto.UserDto;
import com.hmsg.health.bean.domain.UserAccountDo;
import com.hmsg.health.bean.domain.UserDo;

import java.util.Date;
import java.util.List;

import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.One;
import org.apache.ibatis.annotations.Options;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Result;
import org.apache.ibatis.annotations.Results;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import javax.annotation.ParametersAreNonnullByDefault;

@Mapper
public interface UserDao {
    @Select("select * from s_user where id=#{id}")
    UserDo select(@Param("id")Integer id);
    
    @Select("select * from s_user where mobile=#{mobile}")
    UserDo selectByMobile(String mobile);
    
    @Delete("delete from s_user where id=#{userId}")
    int delete(Integer userId);
    
  
    @Insert("<script>" +
    "insert into `s_user` " +
    "  <set>" +
    "    <if test='info.userName != null'>" +
    "     `user_name` = #{info.userName}, " +
    "    </if>" +
    "    <if test='info.mobile != null'>" +
    "      `mobile` = #{info.mobile}, " +
    "    </if>" +
    "    <if test='info.headImage != null'>" +
    "      `head_image` = #{info.headImage}, " +
    "    </if>" +
    "    <if test='info.sex != null'>" +
    "      `sex` = #{info.sex}, " +
    "    </if>" +
    "    <if test='info.isVip != null'>" +
    "      `is_vip` = #{info.isVip}, " +
    "    </if>" +
    "    <if test='info.birth != null'>" +
    "      `birth` = #{info.birth}, " +
    "    </if>" +
    "	<if test='info.createTime != null'>" +
    "      `create_time` = #{info.createTime}, " +
    "    </if>" +
    "	<if test='info.deviceToken != null'>" +
    "      `device_token` = #{info.deviceToken}, " +
    "    </if>" +
    "	<if test='info.vipEnd != null'>" +
    "      `vip_end` = #{info.vipEnd}, " +
    "    </if>" +
	 "	<if test='info.uniqueId != null'>" +
	 "      `unique_id` = #{info.uniqueId}" +
	 "    </if>" +
    "  </set>" +
    "</script>")
	@Options(useGeneratedKeys = true, keyProperty = "id")
	int save(@Param("info") UserDo info);


    @Update({"<script>",
		"UPDATE  s_user",
			"<trim prefix='set' suffixOverrides=','>",
		
		"<if test='user.userName!=null'>",
		"user_name= #{user.userName},",
		"</if>",
		"<if test='user.isVip!=null'>",
		"is_vip= #{user.isVip},",
		"</if>",
		"<if test='user.mobile!=null'>",
		"mobile= #{user.mobile},",
		"</if>",
		"<if test='user.birth!=null'>",
		"birth= #{user.birth},",
		"</if>",
		"<if test='user.deviceToken!=null'>",
		"device_token= #{user.deviceToken},",
		"</if>",
		"<if test='user.vipEnd!=null'>",
		"vip_end= #{user.vipEnd},",
		"</if>",
	
		"<if test='user.headImage!=null'>",
		"head_image= #{user.headImage},",
		"</if>",
		"<if test='user.sex!=null'>",
		"sex= #{user.sex},",
		"</if>",
		"<if test='user.uniqueId!=null'>",
		"unique_id= #{user.uniqueId}",
		"</if>",
		"</trim>",
		"where id = #{user.id}",
		"</script>"})
 	int update(@Param("user") UserDo user);


    //@Select("select * from s_user limit #{firstResult},#{maxResult}")

	@Select("<script>" +
			" select * from s_user " +
			"   where 1=1  " +
			"    <if test='phone != null '>" +
			"     and mobile=#{phone} " +
			"    </if>" +
			"    <if test='unique != null '>" +
			"     and unique_id=#{unique} " +
			"    </if>" +
			"    <if test='time != null '>" +
			"   and  DATE_FORMAT(create_time, '%Y-%m-%d') = DATE_FORMAT(#{time},'%Y-%m-%d') " +
			"    </if>" +
			"    <if test='firstResult != null '>" +
			" limit #{firstResult},#{maxResult}"+
			"    </if>" +
			"</script>")
    List<UserDo> findAll(@Param("phone") String phone,
						 @Param("unique") String unique,
						 @Param("time") String time,
						 @Param("firstResult") Integer firstResult,
						 @Param("maxResult") Integer maxResult);


	@Select("<script>" +
			" select count(1) from s_user" +
			"   where 1=1  " +
			"    <if test='phone != null '>" +
			"     and mobile=#{phone} " +
			"    </if>" +
			"    <if test='unique != null '>" +
			"     and unique=#{unique} " +
			"    </if>" +
			"    <if test='time != null '>" +
			"   and  DATE_FORMAT(create_time, '%Y-%m-%d') = DATE_FORMAT(#{time},'%Y-%m-%d') " +
			"    </if>" +
			"</script>")
    int findCount(@Param("phone") String phone,
				  @Param("unique") String unique,
				  @Param("time") String time);

	@Select("select * from s_user where id=#{id}")
	UserDo findById(@Param("id") Integer id);
}